package com.cm.log_process.log_common.aop;


import com.cm.log_process.log_common.annotation.OperateLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Creator:郭兆鹏
 * Date:2020/1/2
 * 作用:日志打印切面配置
 */
@Order(-1)
@Aspect
@Component
public class LogControllerAspect {
    private static Logger logger = LoggerFactory.getLogger(LogControllerAspect.class);

    //1.配置切点
    @Pointcut("execution (* com.cm.log_process.*.controller.*.*(..))")
    public void controllerAspect(){}

    //2.配置控制器前置通知
    @Before("controllerAspect()")
    public void BeforeOperation(JoinPoint joinPoint){
        logger.info("**************************执行Controller前置通知**************************");
    }

    //3.配置后置通知
    @After("controllerAspect()")
    public void AfterController(JoinPoint joinPoint){
        logger.info("**************************执行Controller后置通知**************************");
        //方法属性获取，用于验证方法
        String name = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName(); //获取执行的方法的名称
        Object[] args = joinPoint.getArgs();
        //初始化自定义注解属性
        String oType = "";
        String oName = "";
        String[] route = {};
        RequestMethod[] requestMethods = {};
        try {
            Class<?> targetClass = Class.forName(name);
            Method[] methods = targetClass.getMethods();
            for (Method method : methods) {
                if(method.getName().equalsIgnoreCase(methodName)){
                    Class<?>[] parameterTypes = method.getParameterTypes();//获取参数列表
                    if(parameterTypes.length==args.length){ //判断参数列表长度是否相等
                        oType = method.getAnnotation(OperateLog.class).oType(); //参数设值
                        oName = method.getAnnotation(OperateLog.class).oName(); //参数设值
                        route = method.getAnnotation(RequestMapping.class).value();//获取路由
                        requestMethods = method.getAnnotation(RequestMapping.class).method();
                        break;
                    }
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("请求方法="+joinPoint.getTarget().getClass().getName()+"."+methodName);
        logger.info("方法类型="+oType);
        logger.info("方法描述="+oName);
        logger.info("请求路由="+Arrays.toString(route));
        logger.info("请求方式="+Arrays.toString(requestMethods));
        logger.info("**************************结束Controller后置通知**************************");
    }


    //4.配置异常抛出捕获通知
    @AfterThrowing(pointcut = "controllerAspect()",throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint,Throwable throwable){
        String name = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        String oType = "";
        String oName = "";
        try {
            Class<?> aClass = Class.forName(name);
            Method[] methods = aClass.getMethods();
            for (Method method : methods) {
                if(method.getName().equalsIgnoreCase(methodName)){
                    Class<?>[] parameterTypes = method.getParameterTypes();//获取参数列表
                    if(parameterTypes.length==args.length){ //判断参数列表长度是否相等
                        oType = method.getAnnotation(OperateLog.class).oType(); //参数设值
                        oName = method.getAnnotation(OperateLog.class).oName(); //参数设值
                        break;
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        logger.warn("**************************异常通知开始**************************");
        logger.warn("出现异常方法名称="+methodName);
        logger.warn("异常方法类型="+oType);
        logger.warn("异常方法作用="+oName);
        if(args.length>0){
            logger.warn("参数列表="+Arrays.toString(args));
        }
        logger.warn("异常原因="+throwable.getCause());
        logger.warn("异常方法栈="+throwable.getMessage());
        logger.warn("**************************异常通知结束**************************");
    }
}
